package com.scs.application.modules.device.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.modules.device.entity.DeviceContractPayable;
import com.scs.application.modules.device.mapper.DeviceContractPayableMapper;
import com.scs.application.modules.device.service.DeviceContractPayableService;
import org.springframework.stereotype.Service;
/**
 *  合同付款计划 服务实现类
 */
@Service
public class DeviceContractPayableServiceImpl extends BaseServiceImpl<DeviceContractPayableMapper, DeviceContractPayable> implements DeviceContractPayableService {

    @Override
    public boolean saveOrUpdate(DeviceContractPayable entity) {
        DeviceContractPayable sum = this.getOne(
                Wrappers.<DeviceContractPayable>query()
                        .eq("work_id", entity.getWorkId())
                        .ne(!entity.isNewRecord(),"id", entity.getId())
                        .select(" sum(payable_ratio) as payable_ratio")
        );
        if (sum == null) {
            sum = new DeviceContractPayable();
        }
        if (sum.getPayableRatio() == null) {
            sum.setPayableRatio(0);
        }
        sum.setPayableRatio(sum.getPayableRatio() + entity.getPayableRatio());
        if (sum.getPayableRatio() > 100) {
            BusinessException.throwError("应付款比例不超过100");
        }
        return super.saveOrUpdate(entity);
    }
}